<!-- 浏览 -->
<template>
  <div id="browsetopic">



    <div class="browsetopic-container" >
      <div class="community-info" @click="pushToCommCenter()">
        <img
          :src="belongedCommunity.communityLcon"
        />
        <img
          :src="belongedCommunity.communityLcon"
        />
        <span>{{
          belongedCommunity.communityName
        }}</span>
        <span>
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-cmtubiaozhizuomoban"></use>
          </svg>
        </span>
      </div>
      <!-- 头图 -->
      <div class="head-img" v-if="getHeadPicUrl(post.postContent)">
        <img :src="getHeadPicUrl(post.postContent)" />
      </div>
      <!-- 头图 -->

      <!-- 标题 -->
      <h1 class="post-title">{{ post.postTitle }}</h1>

      <!-- 发帖人信息 -->
      <div class="poster-info">
        <img :src="posterInfo.userProfile" @click="pushToUserCenter()" />
        <span @click="pushToUserCenter()">{{
          posterInfo.detail.userNickname
        }}</span>
      </div>
      <!-- 发帖人信息 -->

      <!-- 帖子内容 -->
      <div class="post-content" v-html="setContent(post.postContent)"></div>
      <!-- 帖子内容 -->

      <!-- 帖子标签 -->
      <div class="post-tags" v-if="postTags.length > 0">
        <tag
          v-for="(tag, index) in postTags"
          :key="tag + index"
          :closable="false"
          :colored="true"
          >{{ tag.labelName }}</tag
        >
      </div>
      <!-- 帖子标签 -->

      <!-- 帖子相关操作 -->
      <div class="browsetopic-operation">
        <div class="fave-container" @click="handleLike()">
          <favebutton :status="likeStatus"></favebutton>
          <span class="fave-times">{{ likeNumber }}</span>
        </div>
        <div class="fave-container" @click="handleCollect()">
          <collectionbutton :status="collectStatus"></collectionbutton>
          <span class="fave-times">{{ collectNumber }}</span>
        </div>
        <div class="fave-container" @click="openCommentArea()">
          <commentbutton :status="false"></commentbutton>
          <span class="fave-times">评论</span>
        </div>
        <div class="fave-container" @click="handleReport()">
          <reportbutton :status="reportStatus"></reportbutton>
        </div>
        <div
          class="delete-button-container"
          v-if="user.userNo == post.userNo"
          @click="deletePost()"
        >
          <deletebutton :status="false"></deletebutton>
        </div>
        <div
          class="edit-button-container"
          v-if="user.userNo == post.userNo"
          @click="editPost()"
        >
          <editbutton :status="false"></editbutton>
        </div>
      </div>
      <!-- 帖子相关操作 -->

      <!-- 评论区 -->
      <div class="comment-area" v-if="commentAreaVis">
        <div class="comment-area-header">
          <h4>共{{ post.postCommentNumber }}条评论</h4>
          <button class="comment-area-header-op"></button>
        </div>
        <div class="post-comment-container">
          <div class="post-comment-edit-area">
            <commentinput
              v-model="commentContent"
              :commentContent="commentContent"
              v-if="commentInputShow"
            ></commentinput>
          </div>
          <button class="post-comment-button" @click="sendComment()">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-cmfabu"></use>
            </svg>
            发布
          </button>
        </div>
        <div class="comment-info-container">
          <comment
            v-for="comment in comments"
            :key="comment.commentNo"
            :comment="comment"
          ></comment>
        </div>
      </div>
      <!-- 评论区 -->
    </div>
  </div>
</template>

<script>
import {
  getHeadPicUrl,
  intializeComments,
  getLocalTime,
  handleComments,
} from "../utils/utils";
import Favebutton from "../components/common/favebutton.vue";
import Collectionbutton from "../components/common/collectionbutton.vue";
import Reportbutton from "../components/common/littlebutton/reportbutton.vue";
import Commentbutton from "../components/common/littlebutton/commentbutton";
import Commentinput from "../components/common/comment/commentinput.vue";
import Comment from "../components/common/comment/comment.vue";
import Deletebutton from "../components/common/littlebutton/deletebutton.vue";
import Tag from "../components/common/tag/tag";

import { mapGetters, mapMutations } from "vuex";
import {
  handleLike,
  handleCollect,
  handleReport,
} from "../requestsetting/uniOperation";
import Editbutton from "../components/common/littlebutton/editbutton.vue";

export default {
  data() {
    return {
      post: "", //帖子信息
      posterInfo: {
        detail: {
          userNickname: "",
        },
      }, //发帖人信息
      commentContent: "",
      comments: "",
      postlike: [],
      commentAreaVis: false, //评论区显示
      commentInputShow: true, //评论输入框显示
      likeStatus: false, //帖子点赞状态
      collectStatus: false, //帖子收藏状态
      reportStatus: false, //帖子举报状态
      likeNumber: "", //帖子点赞量
      collectNumber: "", //帖子收藏量
      likes: "", //帖子点赞信息
      collections: "", //帖子收藏xinx
      postTags: "", //帖子标签
      belongedCommunity: "", //所属社区
    };
  },

  components: {
    Favebutton,
    Collectionbutton,
    Reportbutton,
    Commentbutton,
    Commentinput,
    Comment,
    Deletebutton,
    Editbutton,
    Tag,
  },

  computed: {
    ...mapGetters(["user"]),
  },

  methods: {
    getPost() {
      console.log("getPost");
      const that = this;
      this.$getApi
        .getPost(this.$route.query.postNo, this.user.userNo)
        .then((res) => {
          that.post = res.data.post;
          that.likeNumber = res.data.post.postLikeNumber;
          that.collectNumber = res.data.post.postCollectionNumber;
          that.likes = res.data.like;
          that.collections = res.data.collection;
          that.getPoster();
          that.getHotComments();
          const _that = that;
          that.$getApi.findRelatedTags(that.post.postNo, 1).then((res) => {
            //获取所有标签
            _that.postTags = res.data.labels;
          });
          that.$getApi.getCommunityDetail(that.post.communityNo).then((res) => {
            //获取所属社区信息
            _that.belongedCommunity = res.data.items[0];
          });
          if (res.data.like[0] != null) that.likeStatus = true;
          if (res.data.collection[0] != null) that.collectStatus = true;
        });
    },
    getHeadPicUrl: getHeadPicUrl,
    setContent(data) {
      //设置帖子内容
      if (data) {
        return data.split("<headpicsplit>")[1];
      }
    },
    async getPoster() {
      // const that = this
      let poster;
      await this.$getApi.getUserInfo(this.post.userNo).then((res) => {
        poster = res.data.userInformation;
      });
      await this.$getApi.getUserDetail(this.post.userNo).then((res) => {
        poster.detail = res.data.userDetails;
      });
      this.posterInfo = poster;
    },
    pushToUserCenter() {
      this.$router.push({
        path: "/usercenter",
        query: { userNo: this.posterInfo.userNo },
      });
    },
    pushToCommCenter(){
      this.$router.push({
        path: "/communitycenter",
        query: { communityno: this.belongedCommunity.communityNo },
      });
    },
    setHeight() {
      let obj = this.$refs.commenteditor;
      obj.style.height = obj.scrollHeight + "px";
    },
    getHotComments() {
      const that = this;
      this.$getApi
        .getHotComments(this.post.postNo, this.user.userNo)
        .then((res) => {
          console.log(res.data);
          let commentstemp = intializeComments(
            res.data.comments,
            res.data.user2,
            res.data.user1
          );
          that.comments = handleComments(
            commentstemp,
            res.data.like,
            res.data.collection
          );
        });
    },
    sendComment() {
      let newdate = new Date();
      let commentTime = newdate.toLocaleString("chinese", { hour12: false });
      commentTime = commentTime.replace(/\//g, "-");
      let commentContentTemp = this.commentContent.replace(/[\n\r]/g, "<br>");
      console.log(commentContentTemp);
      let that = this;
      let config = {
        baseURL: "http://192.168.0.136:8080/",
      };
      this.$getApi.commentCheck(commentContentTemp, config).then((res) => {
        console.log("审核评论", res);
        if (res != 0) {
          that.$toast('请注意友善发言')
        }
      });
      this.commentContent = "";
      this.commentInputShow = false;
      this.$nextTick(() => {
        // dom元素更新后执行，因此这里能正确打印更改之后的值
        this.commentInputShow = true;
      });
      // eslint-disable-next-line no-unused-vars
      this.$getApi
        .addComment(
          this.post.postNo,
          this.user.userNo,
          commentTime,
          commentContentTemp
        )
        .then(() => {
          that.getHotComments();
        });
    },
    openCommentArea() {
      this.commentAreaVis = !this.commentAreaVis;
    },
    handleLike() {
      handleLike(this.user.userNo, this.post.postNo, 1);
      if (this.likeStatus == false) this.likeNumber = this.likeNumber + 1;
      else this.likeNumber = this.likeNumber - 1;
      this.likeStatus = !this.likeStatus;
    },
    handleCollect() {
      handleCollect(this.user.userNo, this.post.postNo, 1);
      if (this.collectStatus == false)
        this.collectNumber = this.collectNumber + 1;
      else this.collectNumber = this.collectNumber - 1;
      this.collectStatus = !this.collectStatus;
    },
    handleReport() {
      handleReport(this.user.userNo, this.post.postNo, 1, getLocalTime());
    },
    editPost() {
      this.setPostContent(this.post);
      this.$router.push({
        path: "/postpage",
        query: { operation: "editpost" },
      });
    },
    deletePost() {
      const that = this;
      // eslint-disable-next-line no-unused-vars
      this.$getApi.deletePost(this.post.postNo).then((res) => {
        that.$router.push("/homepage");
      });
    },
    ...mapMutations(["setPostContent"]),
  },
  created() {
    this.getPost();
  },
};
</script>
<style>
/* #browsetopic{
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    overflow-y: scroll;
} */
.browsetopic-container {
  position: relative;
  width: 690px;
  display: flex;
  align-content: flex-start;
  justify-content: center;
  flex-wrap: wrap;
  margin: 4rem auto;
  transition: all 0.5s;
  z-index: 10;
}

.browsetopic-container > .community-info {
  border-radius: 4px;
  width: calc(100% - 30px);
  padding: 10px 15px;
  margin: 10px 0 30px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  position: relative;
  overflow: hidden;
  cursor: pointer;
}
.browsetopic-container > .community-info > img:nth-child(1) {
  height: 40px;
  width: 40px;
  border-radius: 4px;
  z-index: 12;
}
.browsetopic-container > .community-info > img:nth-child(2) {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 1000%;
  filter: blur(40px) contrast(70%) brightness(100%);
  z-index: 11;
  transform: scale(1.3);
}
.browsetopic-container > .community-info > span:nth-child(3) {
  padding: 0 10px;
  color: rgba(255, 255, 255, 0.918);
  z-index: 12;
}
.browsetopic-container > .community-info > span:nth-child(4){
  position: absolute;
  right: 0;
  padding: 0 10px;
  z-index: 12;
}
.browsetopic-container > .community-info > span:nth-child(4)>.icon {
  font-size: 20px;
  vertical-align: middle;
  color: rgba(255, 255, 255, 0.673);
  z-index: 12;
  transition: all 0.2s;
}
.browsetopic-container>.community-info:hover span:nth-child(4)>.icon{
  color: rgb(80, 80, 255);
  transition: all 0.2s;
}
.browsetopic-container>.community-info:hover span:nth-child(3){
  color: rgb(246, 246, 255);
  transition: all 0.2s;
}

.head-img {
  width: 100%;
}
.head-img img {
  width: 100%;
}

/* .belonged-community{
  width: 100%;
  padding: 10px 10px;
} */

.post-content {
  width: 100%;
}
.post-content img {
  width: 80%;
  margin: 10px 10%;
  max-height: 330px;
  background-position: center;
}
h1.post-title {
  font-size: 26px;
  line-height: 2;
  width: 100%;
  word-wrap: break-word;
  margin: 20px 0;
}

/* 发帖人信息 */
.poster-info {
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
  /* margin-bottom: 20px; */
  justify-content: flex-start;
  cursor: pointer;
  position: relative;
}
.poster-info > img {
  width: 40px;
  height: 40px;
  border-radius: 20px;
}
.poster-info > span {
  padding: 0 10px;
}
/* 发帖人信息 */

.post-content h2 {
  font-size: 22px;
  color: #121212;
}
.post-content h3 {
  font-size: 18px;
  color: #121212;
}
.post-content p {
  color: #121212;
  font-size: 16px;
}
.post-content span {
  color: #121212;
  font-size: 16px;
}
.post-content blockquote {
  font-size: 18px;
  width: 100%;
  margin: 10px 0;
  border-left: 5px rgb(80, 80, 255) solid;
  padding: 0 10px;
  color: rgb(57, 57, 57);
}

/* 帖子标签 */
.browsetopic-container > .post-tags {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  flex-wrap: wrap;
}
/* 帖子标签 */

/* 操作栏 */
.browsetopic-operation {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  line-height: 1.67;
  font-size: 14px;
}
.browsetopic-operation > .delete-button-container {
  position: absolute;
  right: 0;
}
.browsetopic-operation > .edit-button-container {
  position: absolute;
  right: 50px;
}
/* 评论区 */
.comment-area {
  position: relative;
  width: 100%;
  margin-top: 1rem;
  padding: 20px 20px;
  background-color: white;
  box-shadow: rgba(205, 230, 255, 0.5) 0px 8px 24px;
}

.comment-area-header {
  position: relative;
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
  border-bottom: 1px rgb(241, 247, 255) solid;
  margin-bottom: 10px;
}
.comment-area-header > h4 {
  margin: 0;
}
.comment-area-header-op {
  position: absolute;
  right: 0;
  border: none;
  outline: none;
  background: transparent;
}
.post-comment-container {
  position: relative;
  width: 100%;
  min-height: 30px;
}
.post-comment-edit-area {
  position: relative;
  width: 90%;
}
.post-comment-button {
  position: absolute;
  height: 30px;
  width: calc(10% - 8px);
  right: 0;
  bottom: 0;
  background-color: transparent;
  transition: all 0.2s;
  padding: 0;
  border: none;
  outline: none;
  background-color: antiquewhite;
  margin-bottom: 4px;
  border-radius: 2px;
  color: white;
}
.post-comment-button:hover {
  background-color: rgb(255, 211, 169);
  transition: all 0.2s;
}
.post-comment-button > .icon {
  font-size: 12px;
}

.comment-info-container {
  margin-top: 1rem;
  width: 100%;
}

.fave-times {
  font-size: 14px;
  color: rgb(117, 117, 117);
  height: 30px;
  width: 40px;
  line-height: 30px;
  margin-left: -15px;
}
</style>
